﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sitecore.Data.Items;

namespace Workflow.Alerts.Sublayouts.ProfileSettings
{
    public partial class ContractorDtls : BaseControl, IContractorDtlsView
    {
        User user;

        #region IContractorDtlsView Members

        String IContractorDtlsView.Name
        {
            set
            {
                txtName.Text = value;
                lblCompanyName.Text = value;
            }
            get { return txtName.Text; }
        }

        String IContractorDtlsView.PostAddress
        {
            set { txtPostAddress.Text = value; }
            get { return txtPostAddress.Text; }
        }

        String IContractorDtlsView.PostCode
        {
            set { txtPostCode.Text = value; }
            get { return txtPostCode.Text; }
        }

        String IContractorDtlsView.City
        {
            set { txtPlaats.Text = value; }
            get { return txtPlaats.Text; }
        }

        String IContractorDtlsView.Email
        {
            set { txtEmail.Text = value; }
            get { return txtEmail.Text; }
        }

        String IContractorDtlsView.FirstName
        {
            set { txtFirstName.Text = value; }
            get { return txtFirstName.Text; }
        }

        String IContractorDtlsView.LastName
        {
            set { txtLastName.Text = value; }
            get { return txtLastName.Text; }
        }

        String IContractorDtlsView.Phone1
        {
            set { txtPhone1.Text = value; }
            get { return txtPhone1.Text; }
        }

        String IContractorDtlsView.Phone2
        {
            set { txtPhone2.Text = value; }
            get { return txtPhone2.Text; }
        }



        public String ExceptionField { get; set; }

        public Int32? Id
        {
            get
            {
                if ((Session[WorkflowConstants.CurrentUser] as User).Type == WorkflowEnums.UserType.Contractor)
                {
                    return (Session[WorkflowConstants.CurrentUser] as User).TypeId;
                }
                if (Request.QueryString["opId"] != null && Int32.TryParse(Request.QueryString["opId"], out id))
                {
                    return id;
                }
                return null;
            }
            set
            {
                if (value.HasValue)
                    id = value.Value;

            }
        }

        public Boolean IsDisabled
        {
            get
            {
                return !chkDisabled.Checked;
            }
            set
            {
                chkDisabled.Checked = !value;
            }
        }


        public event IContractorDtlsViewLoad OnPageLoad;
        public event IContractorDtlsViewUpdate OnUpdate;

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            PresentationManager.RegisterView(typeof(ContractorDtlsPresenter), this, new WebSessionProvider());
            bool idHasValue = Id.HasValue;
            if (!IsPostBack && idHasValue && OnPageLoad != null)
            {
                OnPageLoad();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (OnUpdate != null)
            {
                if (OnUpdate())
                {
                    var path = Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item.Parent);
                    Response.Redirect(path);
                }
                else
                {
                    if (ExceptionField == "login")
                        lblLoginValidator.Visible = true;
                    else if (ExceptionField == "email")
                        lblEmailValidator.Visible = true;
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            var path = Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item.Parent);
            Response.Redirect(path);
        }

        #endregion

        #region Fields
        private int id;
        #endregion
    }
}